<?php
class ControllerAddonImport extends Controller
{
	private $error = array();
	
	public function index()
	{
		$this->load->language('addon/order');
		$this->data = array_merge($this->data, $this->language->getData());
		$type = $this->request->get['type'];
		
		$this->data['heading_title'] = "Import Voucher List";
		$this->load->model("addon/order");
		$orderid = $this->request->get['orderid'];
		if($orderid=="")
			$this->getList();
		else
			$this->getForm();
	}
	
	public function view()
	{
		$this->data['heading_title'] = "Import Voucher";
		$orderid = $this->request->get['orderid'];
		$this->load->model("addon/order");
		$this->load->helper('image');
		$this->data = $this->model_addon_order->getItem($orderid);
		if($this->data['order']['status']=='')
			$this->data['order']['text_active'] = "Pending";
		else
			$this->data['order']['text_active'] = "Completed";
		foreach($this->data['detail'] as $key => $item)
		{
			$imagepreview = "<img width=100 src='".HelperImage::resizePNG($item['imagepath'], 180, 180)."' >";
			$this->data['detail'][$key]['imagepreview'] = $imagepreview;
		}
		
		$this->load->language('addon/order');
		$this->data = array_merge($this->data, $this->language->getData());
		
		$this->id='content';
		$this->template="addon/import_view.tpl";
		//$this->layout="layout/center";
		$this->render();
	}
	
	private function getListProduct()
	{
		$this->load->model("core/media");
		$this->load->model("core/sitemap");
		$matrix = array();
		$this->model_core_sitemap->getTree("category", $matrix);
		$listsitemap = $this->string->matrixToArray($matrix,"sitemapid");
		$queryoptions['mediaparent'] = '%';
		$queryoptions['mediatype'] = '%';
		$queryoptions['refersitemap'] = $listsitemap;
		
		$medias = $this->model_core_media->getPaginationList($queryoptions);
		return $medias;
	}
	
	private function getList() 
	{
		$this->data['insert'] = $this->url->http('addon/import/insert');
		$this->data['delete'] = $this->url->http('addon/import/delete');		
		
		$this->data['orders'] = array();
		$where ="";
		$data=$this->request->get;
		if($data['productname']!="")
		{
			$w = " AND mediaid in (Select mediaid from media Where title like '%".$data['productname']."%')  AND type ='import'";
			$list = $this->model_addon_order->getListOrderProduct($w);
			$arr = $this->string->matrixToArray($list,"orderid");
			$where .= " AND orderid in('".implode("','",$arr)."')";
		}
		
		if($data['orderidfiter']!="")
			$where .= " AND orderid like '%".$data['orderidfiter']."%'";
		if($data['createby']!="")
			$where .= " AND createby like '%".$data['createby']."%'";
		if($data['supplier']!="")
			$where .= " AND supplier like '%".$data['supplier']."%'";
		if($data['startdate']!="")
			$where .= " AND orderdate >='".$this->date->formatViewDate($data['startdate'])."'";
		if($data['enddate']!="")
			$where .= " AND orderdate <='".$this->date->formatViewDate($data['enddate'])."'";
		$where .= " AND `type` = 'import' ORDER BY  `order`.`orderdate` DESC ";
		$rows = $this->model_addon_order->getListOrder($where);
		//Page
		$page = $this->request->get['page'];		
		$x=$page;		
		$limit = 20;
		$total = count($rows); 
		// work out the pager values 
		$this->data['pager']  = $this->pager->pageLayout($total, $limit, $page); 
		
		$pager  = $this->pager->getPagerData($total, $limit, $page); 
		$offset = $pager->offset; 
		$limit  = $pager->limit; 
		$page   = $pager->page;
		for($i=$offset;$i < $offset + $limit && count($rows[$i])>0;$i++)
		{
			$this->data['orders'][$i] = $rows[$i];
			$this->data['orders'][$i]['link_active'] = $this->url->http('addon/order/check&orderid='.$this->data['orders'][$i]['orderid']);
			if($this->data['orders'][$i]['status']=='')
				$this->data['orders'][$i]['text_active'] = "Pending";
			else
				$this->data['orders'][$i]['text_active'] = "Completed";
		}
		
		
		$this->id='content';
		$this->template="addon/import_list.tpl";
		$this->layout="layout/center";
		$this->render();
	}
	
	public function updatestatus()
	{
		$this->load->model("addon/order");
		$orderid = $this->request->get['orderid'];
		//$this->data = $this->model_addon_order->getItem($orderid);
		$data['orderid'] = $orderid;
		$data['status'] = "checked";
		$this->model_addon_order->updateStatus($data);
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
	}
	
	public function insert()
	{
		$this->getForm();
	}
	
	private function getForm()
	{
		$this->load->model("addon/order");
		$this->load->model("core/sitemap");
		$this->load->model("core/media");
		$this->data['order']['orderdate'] = $this->date->formatMySQLDate($this->date->getToday());
		
		$orderid = $this->request->get['orderid'];
		if($orderid)
		{
			$this->data =array();
			$this->data=$this->model_addon_order->getItem($orderid);
			$this->data['order']['orderdate'] = $this->date->formatMySQLDate($this->data['order']['orderdate']);
		}
		
		$this->data['treecategory'] = array();
		$this->model_core_sitemap->getTree("category",$this->data['treecategory']);
		$this->data['medias'] = $this->model_core_media->getList();
		foreach($this->data['medias'] as $key => $item)
		{
			$this->data['medias'][$key]['instock'] = $this->model_core_media->getInstock($item['mediaid']);
		}
		
		$this->id='content';
		$this->template="addon/import_form.tpl";
		$this->layout="layout/center";
		$this->render();
	}
	
	public function savepost()
	{
		$data = $this->request->post;
		
		$this->data['errors'] = $this->validate($data);
		if(count($this->data['errors'])==0)
		{
			$data['orderdate'] = $this->date->formatViewDate($data['orderdate']);
			$data['type'] = 'import';
			$this->load->model("addon/order");
			$orderid="";
			if($data['orderid']=="")
				$orderid = $this->model_addon_order->insert($data);
			else
				$orderid = $this->model_addon_order->update($data);
			//Remove order detail
			if(count($data['remove']))
				foreach($data['remove'] as $item)
				{
					$this->model_addon_order->removeOrderProduct($item);	
				}
			//$this->model_addon_order->removeOrderProduct($orderid);	
			//Save Orderdetail
			
			foreach($data['product'] as $key => $item)
			{
				$dataorder['id'] = $data['id'][$key];
				$dataorder['orderid'] = $orderid;
				$dataorder['mediaid'] = $item;
				$dataorder['quantity'] = str_replace("," ,"",$data['quantity'][$key]);
				$dataorder['price'] = str_replace("," ,"",$data['price'][$key]);
				$dataorder['discount'] = str_replace("," ,"",$data['discount'][$key]);
				$dataorder['subtotal'] = $dataorder['quantity']*$dataorder['price'];
				$dataorder['type'] = $data['type'];
				$this->model_addon_order->saveOrderProduct($dataorder);	
			}
			$this->model_addon_order->updateTotal($orderid);
			$this->data['output'] =$orderid;
		}
		else
			$this->data['output'] = "false";
			
		$this->id='import';
		$this->template='common/output.tpl';	
		$this->render();
		
	}
	
	function validate($data)
	{
		$err = array();
		if($data['createby']=="")
			$err['createby'] ="Create by not null";
		if($data['supplier']=="")
			$err['supplier'] ="Supplier not null";
		if($data['orderdate']=="")
			$err['orderdate'] ="Date by not null";
		return $err;
	}
}
?>